package org.farm2.auth.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.farm2.auth.dao.PostActionsDao;
import org.farm2.auth.dao.UserPostDao;
import org.farm2.auth.domain.PostActions;
import org.farm2.auth.domain.UserPost;
import org.farm2.base.db.FarmDbFields;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.base.exception.FarmExceptionUtils;
import org.farm2.auth.dao.PostDao;
import org.farm2.auth.domain.Post;
import org.farm2.auth.service.PostServiceInter;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.db.commons.DBRule;
import org.farm2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;


@Service
@Slf4j
public class PostServiceImpl implements PostServiceInter {
    @Autowired
    private PostActionsDao postActionsDao;
    @Autowired
    private UserPostDao userPostDao;
    @Autowired
    private PostDao postDao;

    @Transactional
    @Override
    public Post insertPostEntity(Post post) {
        FarmDbFields.initInsertBean(post, FarmUserContextLoader.getCurrentUser());

        postDao.insert(post);


        return post;
    }

    @Transactional
    @Override
    public Post editPostEntity(Post post) {
        Post savePost = getPostById(post.getId());
        FarmExceptionUtils.throwNullEx(savePost, I18n.msg("角色不存在:?", post.getId()));
        savePost.setState(post.getState());
        savePost.setNote(post.getNote());
        savePost.setName(post.getName());
        savePost.setKeyid(post.getKeyid());
        FarmDbFields.initUpdateBean(savePost, FarmUserContextLoader.getCurrentUser());
        postDao.update(savePost);
        return savePost;
    }

    @Transactional
    @Override
    public Post getPostById(String id) {
        return postDao.findById(id);
    }

    @Override
    public List<Post> getPosts(DataQuery query) {
        return postDao.queryData(query.setCount(false)).getData(Post.class);
    }


    @Transactional
    @Override
    public DataResult searchPost(DataQuery query) {
        DataResult result = postDao.queryData(query);
        return result;
    }

    @Override
    public int getPostNum(DataQuery query) {
        return postDao.countData(query);
    }


    @Transactional
    @Override
    public void delPost(String id) {

        postActionsDao.delete(DBRule.getInstance("POSTID", id, "=").getRules());

        userPostDao.delete(DBRule.getInstance("POSTID", id, "=").getRules());
        postDao.deleteById(id);
    }

    @Override
    public int getNum(DataQuery query) {
        return postDao.countData(query);
    }

    @Transactional
    @Override
    public void savePostActions(String postid, List<String> actionsids) {
        postActionsDao.delete(DBRule.getInstance("POSTID", postid, "=").getRules());
        for (String actionsid : actionsids) {
            PostActions pactions = new PostActions();
            pactions.setActionsid(actionsid);
            pactions.setPostid(postid);
            postActionsDao.insert(pactions);
        }
    }

    @Override
    public List<String> findActions(String postId) {
        List<String> actions = postActionsDao.findByPostId(postId);
        return actions;
    }

    @Transactional
    @Override
    public void savePostUser(String loginname, List<String> posts) {
        userPostDao.delete(DBRule.getInstance("USERKEY", loginname, "=").getRules());
        if (posts != null) {
            for (String postid : posts) {
                UserPost spost = new UserPost();
                spost.setUserkey(loginname);
                spost.setPostid(postid);
                userPostDao.insert(spost);
            }
        }
    }

    @Override
    public List<String> getUserPost(String loginname) {
        DataResult result = userPostDao.queryData(DataQuery.getInstance().setPageSizeAll().setCount(false).addRule(new DBRule("USERKEY", loginname, "=")));
        List<String> ids = result.getData().stream().map(new Function<Map<String, Object>, String>() {
            @Override
            public String apply(Map<String, Object> stringObjectMap) {
                return (String) stringObjectMap.get("POSTID");
            }
        }).toList();
        return ids;
    }

}
